package com.warmlight.voicepacket.widget;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.adapter.FloatPlayListAdapter;
import com.warmlight.voicepacket.data.UserCenterData;
import com.warmlight.voicepacket.dbdata.DBFloatPlaylist;
import com.warmlight.voicepacket.dbmanager.DBFloatPlayListDaoHelp;
import com.warmlight.voicepacket.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jason on 2018/5/25.
 */

public class FloatPlayList extends RelativeLayout {

    private Context mContext;
    private RelativeLayout mContainer;
    private ListView lv_playlist;
    private FloatPlayListAdapter mAdapter;
//    private List<FloatPlayListData> mList;
    private List<DBFloatPlaylist> mList;

    public FloatPlayList(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public FloatPlayList(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public FloatPlayList(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public FloatPlayList(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        init();
    }

    private void init(){
//        mList = DBHelper.getInstance(mContext).queryAllList();
        if (SPUtils.isLogined()){
            //获取用户的数据
            String userCenter = SPUtils.getString(SPUtils.USER_CENTER);
            if (!TextUtils.isEmpty(userCenter)){
                UserCenterData userCenterData = new Gson().fromJson(userCenter, UserCenterData.class);
                mList = change(userCenterData);
                if (mList == null){
                    mList = new ArrayList<>();
                }
            }else{
                mList = new ArrayList<>();
            }
        }else{

            mList = DBFloatPlayListDaoHelp.loadAll(mContext);
        }


        mContainer = (RelativeLayout) View.inflate(mContext, R.layout.float_playlist_content, this);
        lv_playlist = (ListView) mContainer.findViewById(R.id.lv_playlist);
        //TODO 这里可以根据工具类方法直接查询播放列表数据库信息，读取到列表
        mAdapter = new FloatPlayListAdapter(mContext,mList);
        lv_playlist.setAdapter(mAdapter);

        lv_playlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(mPlaylistClickListener != null){
                    mPlaylistClickListener.ItemClicked(mList == null ? null : mList.get(i));
                }
            }
        });

    }

    private PlaylistClickListener mPlaylistClickListener;

    public List<DBFloatPlaylist> change(UserCenterData userCenterData) {
        List<UserCenterData.ListBean> list = userCenterData.getList();
        List<DBFloatPlaylist> mList = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            UserCenterData.ListBean listBean = list.get(i);
            DBFloatPlaylist d = new DBFloatPlaylist();
            d.setId(Long.valueOf(listBean.getId()));
            d.setList_count(listBean.getAudio_num());
            d.setList_img_path(listBean.getAvator());
            d.setList_name(listBean.getName());
            mList.add(d);
        }
        return mList;
    }

    public void refresh(UserCenterData userCenterData) {
        List<UserCenterData.ListBean> list = userCenterData.getList();
        List<DBFloatPlaylist> mList = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            UserCenterData.ListBean listBean = list.get(i);
            DBFloatPlaylist d = new DBFloatPlaylist();
            d.setId(Long.valueOf(listBean.getId()));
            d.setList_count(listBean.getAudio_num());
            d.setList_img_path(listBean.getAvator());
            d.setList_name(listBean.getName());
            mList.add(d);
        }
        if(mAdapter!=null){
//            mList = DBHelper.getInstance(mContext).queryAllList();
            this.mList = mList;
            mAdapter.setList(this.mList);
        }
    }

    public interface PlaylistClickListener{
        void ItemClicked(DBFloatPlaylist data);
    }
    public void setOnPlaylistClickListener(PlaylistClickListener listener){
        mPlaylistClickListener = listener;
    }

    //刷新列表
    public void notifyPlaylist(){
        if(mAdapter!=null){
//            mList = DBHelper.getInstance(mContext).queryAllList();
            if (SPUtils.isLogined()){
                //获取用户的数据
                String userCenter = SPUtils.getString(SPUtils.USER_CENTER);
                if (!TextUtils.isEmpty(userCenter)){
                    UserCenterData userCenterData = new Gson().fromJson(userCenter, UserCenterData.class);
                    mList = change(userCenterData);
                    if (mList == null){
                        mList = new ArrayList<>();
                    }
                }else{
                    mList = new ArrayList<>();
                }
            }else{

                mList = DBFloatPlayListDaoHelp.loadAll(mContext);
            }
            mAdapter.setList(mList);
        }
    }

}
